package fileTool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/** 
* @author  作者:袁云 
* @date 创建时间：2017年5月21日 上午12:15:16
*/
public class Tool {
	
	private static final String WORKSPACE = "D:/java/workspace";
	private static final String IGNORE_FILE	=".gitignore";
	private static final String[] IGNORE_FILES = {
			".classpath",".project",".mymetadata",
			"/target/","/bin/","/classes/","/.settings/","/.myeclipse/"
	};
	
	private static final String[] SKIP_FOLDERS = {
		".git",".metadata",".recommenders","RemoteSystemsTempFiles","Servers"
	};
	
	public static void main(String[] args) throws IOException {
		
		File folder = new File(WORKSPACE);
		if (!folder.exists() || !folder.isDirectory()){
			return;
		}
		
		int count = 0;
		for(File f:folder.listFiles()){
			if(f.isDirectory() && !isSkip(f.getName())){//处理
				File ignoreFile = new File(f.getAbsolutePath()+File.separator+IGNORE_FILE);
				System.out.println(ignoreFile.getAbsolutePath());
				if(!ignoreFile.exists()){
					if(ignoreFile.createNewFile()){
						FileWriter writer = new FileWriter(ignoreFile);
						for(String ignore:IGNORE_FILES){
							writer.write(ignore+"\n");
						}
						writer.flush();
						writer.close();
						count++;
					}
				}
			}
		}
		System.out.println("共创建"+count+"个.gitignore文件");
		
	}
	
	public static boolean isSkip(String name){
		for(String skipName:SKIP_FOLDERS){
			if(skipName.equals(name)){
				return true;
			}
		}
		return false;
	}
	
}
